//
//  MKHandleCItyData.m
//  ChooseLocation
//
//  Created by Apple on 14/11/25.
//  Copyright (c) 2014年 Apple. All rights reserved.
//

#import "MKHandleCityData.h"
#import "MKCity.h"

@implementation MKHandleCityData


#pragma mark - 转拼音
- (NSMutableArray *)changeHanZiToPinYinWithArray:(NSMutableArray *)originalArray
{
    // 汉字转拼音
    NSMutableArray * tempPinyinArray = [NSMutableArray array];
    for (int i = 0; i < originalArray.count; i++) {
        NSMutableString * ms = [[NSMutableString alloc] initWithString:originalArray[i]];
        
        MKCity * city = [[MKCity alloc] init];
        city.cityName = [originalArray objectAtIndex:i];
        city.letter = [self ChangeHanziToPinYin:ms];
        
        // 放在数组里
        [tempPinyinArray addObject:city];
    }
    
    // 排序
    NSMutableArray * sortArray = [NSMutableArray array];
    sortArray = [NSMutableArray arrayWithArray:[tempPinyinArray sortedArrayUsingFunction:nickNameSort context:NULL]];

    // 开头字母初始化
    NSMutableArray * arrayForArrays = [NSMutableArray array];
    self.sectionHeadsKeys = [NSMutableArray array];
    BOOL checkValueAtIndex = NO;
    NSMutableArray * tempArrForGrouping = [NSMutableArray array];
    for (int i = 0; i < sortArray.count; i++) {
        MKCity * city = sortArray[i];
        NSMutableString * strChar = (NSMutableString *)city.letter;
        // 取首字母
        NSString * str = [strChar substringToIndex:1];
        // 检查数组内是否有该首字母，没有就创建
        if (![self.sectionHeadsKeys containsObject:[str uppercaseString]]) {
            // 不存在就添加进去
            [self.sectionHeadsKeys addObject:[str uppercaseString]];
            tempArrForGrouping = [[NSMutableArray alloc] initWithObjects:nil];
            checkValueAtIndex = NO;
        }
        
        // 有就把数据添加进去，注意这里 不能连续判断，而且前面的创建必须在前面
        if ([self.sectionHeadsKeys containsObject:[str uppercaseString]]) {
            [tempArrForGrouping addObject:[sortArray objectAtIndex:i]];
            if (checkValueAtIndex == NO) {
                [arrayForArrays addObject:tempArrForGrouping];
                checkValueAtIndex = YES;
            }
        }
    }
    NSMutableArray * array = [NSMutableArray arrayWithObjects:self.sectionHeadsKeys, arrayForArrays, sortArray, nil];
    
    for (int i = 0; i < sortArray.count; i++) {
        MKCity * city = [sortArray objectAtIndex:i];
        NSLog(@"%@____%@", city.cityName, city.letter);
    }
    
    // 将arrayForArrays归档
    // 先转为Data，然后将Data归档
    NSData * sortData = [NSKeyedArchiver archivedDataWithRootObject:array];
    [[NSUserDefaults standardUserDefaults] setObject:sortData forKey:@"sortArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return array;
}

// 汉字转拼音，同时注意有一些是会错误的，需手动设置
// http://www.daxueit.com/article/4447.html
- (NSString *)ChangeHanziToPinYin:(NSString *)sourceString
{
    if ([sourceString isEqualToString:@""]) {
        return sourceString;
    }
    
    NSMutableString * source = [sourceString mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);
    
    NSString * firstItemStr = [(NSString *)sourceString substringToIndex:1];
    
    if ([firstItemStr compare:@"长"] == NSOrderedSame) {
        [source replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chang"];
    }
    
    if ([firstItemStr compare:@"沈"] == NSOrderedSame) {
        [source replaceCharactersInRange:NSMakeRange(0, 4) withString:@"shen"];
    }
    
    if ([firstItemStr compare:@"厦"] == NSOrderedSame) {
        [source replaceCharactersInRange:NSMakeRange(0, 3) withString:@"xia"];
    }
    
    if ([firstItemStr compare:@"地"] == NSOrderedSame) {
        [source replaceCharactersInRange:NSMakeRange(0, 3) withString:@"di"];
    }
    
    
    if ([firstItemStr compare:@"重"] == NSOrderedSame) {
        [source replaceCharactersInRange:NSMakeRange(0, 5) withString:@"chong"];
    }
    
    return source;
    
}


// 按字母排序方法
NSInteger nickNameSort(id user1, id user2, void *context)
{
    MKCity * u1, * u2;
    // 类型转换
    u1 = (MKCity *)user1;
    u2 = (MKCity *)user2;
    return [u1.letter localizedCompare:u2.letter];
}

// 去掉city的市、站
- (NSString *)cityFormatWithString:(NSString *)cityString
{
    NSString *resultString = cityString;
    if ([cityString containsString:@"市"]) {
        NSArray * cityArray = [cityString componentsSeparatedByString:@"市"];
        resultString = cityArray[0];
    }
    
    if ([cityString containsString:@"站"]) {
        NSArray * cityArray = [cityString componentsSeparatedByString:@"站"];
        resultString = cityArray[0];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:resultString forKey:@"showCity"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    return resultString;
}

@end
